package org.shagin.jfin.ui.actions;

import javax.swing.ActionMap;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.shagin.jfin.budget.BudgetXMLReader;
import org.shagin.jfin.budget.BudgetXMLWriter;
import org.shagin.jfin.core.CoreUtils;
import org.shagin.jfin.core.Debug;
import org.shagin.jfin.ui.JMainFrame;
import org.shagin.jfin.ui.editor.budget.BudgetEditor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class MainFrameActions.
 */
public class MainFrameActions {
    private static final Logger log = LoggerFactory
            .getLogger(MainFrameActions.class);

    public static final String ACTION_REFRESH = "refresh";
    public static final String ACTION_RELOAD = "reload";
    public static final String ACTION_EDIT = "edit";
    public static final String ACTION_SAVE = "save";

    private JMainFrame mainFrame;
    private String fileName = "D:/p150820112041/scm/p150820112041/etc/shagin1.xml";

    /**
     * Instantiates a new main frame actions.
     * 
     * @param mainFrame the main frame
     */
    public MainFrameActions(JMainFrame mainFrame) {
        CoreUtils.checkNotNull(mainFrame, "mainFrame");
        this.mainFrame = mainFrame;
    }

    /**
     * Refresh.
     */
    @Action
    public void refresh() {
        Debug.traceEnter(log);
        recalculate();
    }

    /**
     * Reload.
     */
    @Action
    public void reload() {
        Debug.traceEnter(log);
        refreshBudget();
    }

    /**
     * Edits the.
     */
    @Action
    public void edit() {
        Debug.traceEnter(log);
        new BudgetEditor(mainFrame.getSheetTable().getBudget()).edit(JMainFrame
                .getInstance());
    }

    /**
     * Refresh data source.
     */
    public void refreshBudget() {
        mainFrame.getSheetTable().setBudget(
                new BudgetXMLReader(fileName).read());
    }

    @Action
    public void save() {
        Debug.traceEnter(log);
        new BudgetXMLWriter(fileName).write(mainFrame.getSheetTable()
                .getBudget());
    }

    /**
     * Recalculate data source.
     */
    public void recalculate() {
        mainFrame.getSheetTable().recalculate();
    }

    /**
     * Gets the resource map.
     * 
     * @return the resource map
     */
    public ResourceMap getResourceMap() {
        return Application.getInstance().getContext()
                .getResourceMap(MainFrameActions.class);
    }

    /**
     * Gets the action map.
     * 
     * @return the action map
     */
    public ActionMap getActionMap() {
        return Application.getInstance().getContext().getActionMap(this);
    }
}
